﻿<%@ Page MasterPageFile="~/Site.Master" Language="C#" AutoEventWireup="true" CodeBehind="CartViewGroup.aspx.cs" Inherits="RabbitShop.UI.CartViewGroup" %>

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
    
    <form id="form1" action="/Action/OrderOneShop.aspx" method="POST">

    <script type="text/javascript">
        
        function CheckShop(shopId) {
            var allInputs = document.getElementsByTagName("input");
            for (var i = 0; i < allInputs.length; i++) {
                if (allInputs[i].type == "checkbox") {
                    allInputs[i].checked = false;
                    allInputs[i].disabled = true;
                }
            }
            var inputs = document.getElementsByName("check" + shopId);
            for (var j = 0; j < inputs.length; j++) {
                inputs[j].disabled = false;
                inputs[j].checked = true;
            }
        }
        function CheckProduct(shopId, productId) {
            var input = document.getElementById("check"+productId);
            if (input.disabled) {
                return;
            }
            var inputShop = document.getElementById("radio"+shopId);
            if (inputShop.checked) {
                return;
            }

            inputShop.checked = true;
            
            var allInputs = document.getElementsByTagName("input");
            for (var i = 0; i < allInputs.length; i++) {
                if (allInputs[i].type == "checkbox" && allInputs[i].name!="check"+shopId) {
                    allInputs[i].checked = false;
                    allInputs[i].disabled = true;
                }
            }
        }
        function ChangeQuantity(type, inputname) {
            var input = document.getElementById(inputname);
            var qty = parseInt(input.value);
            if (type == 'sub') {
                if (qty > 1) {
                    input.value = (qty - 1);
                }
            } else {
                input.value = (qty + 1);
            }
        }
        function getCookie(c_name) {
            if (document.cookie.length > 0) {
                c_start = document.cookie.indexOf(c_name + "=");
                if (c_start != -1) {
                    c_start = c_start + c_name.length + 1;
                    c_end = document.cookie.indexOf(";", c_start);
                    if (c_end == -1) c_end = document.cookie.length;
                    return unescape(document.cookie.substring(c_start, c_end));
                }
            }
            return "";
        }
        function SubmitCart() {
            var uid = getCookie("uid");
            if (uid == "") {
                window.location = "UserLogon.aspx";
                return;
            }

            var shopId;
            var inputRadios = document.getElementsByName("orderShop");
            for (var j = 0; j < inputRadios.length; j++) {
                if (inputRadios[j].checked) {
                    shopId = inputRadios[j].value;
                    break;
                }
            }

            var productAndQuantityList = [];
            var inputChecks = document.getElementsByName("check" + shopId);
            for (var k = 0; k < inputChecks.length; k++) {
                if (inputChecks[k].checked) {
                    var productId = inputChecks[k].value;
                    var quantity = document.getElementById("quantity" + productId).value;
                    var productAndQuantity = { "ProductId": productId, "Quantity": quantity };
                    productAndQuantityList.push(productAndQuantity);
                }
            }

            var data = { "ShopId": shopId, "CartItemList": productAndQuantityList };

            var xhr = new XMLHttpRequest();
            xhr.open(form1.method, form1.action, true);
            xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');

            xhr.send(JSON.stringify(data));

            xhr.onloadend = function () {
                if (xhr.responseText == "true") {
                    window.location = "OrderMake.aspx";
                } else {
                    alert(xhr.responseText);
                }
            };
        }
    </script>
    
    <section class="contact">
        <header>
            <h3>Cart:</h3>
        </header>
        
        <table>
            <tr>
                <td>
                    
                </td>
            </tr>
            
    <asp:Repeater runat="server" ID="shopRepeater" >
        <ItemTemplate>
            
            <tr>
                <td>
                    <input type="radio" id="radio<%#Eval("ShopInfo.ShopId")%>" name="orderShop" value="<%#Eval("ShopInfo.ShopId")%>" onclick="CheckShop('<%#Eval("ShopInfo.ShopId")%>')" />
                    <a href="/UI/ProductList.aspx"><%#Eval("ShopInfo.NickName") %></a>
                </td>
            </tr>
            <tr>
                <td>
                    <table style="text-align: center;">
                        <tr>
                            <td>商品</td>
                            <td>名称</td>
                            <td>标题</td>
                            <td>单价</td>
                            <td>数量</td>
                            <td>小计</td>
                            <td>操作</td>
                        </tr>

                <asp:Repeater runat="server" ID="productRepeater" >
                    <ItemTemplate>
                        <tr>
                            <td>
                                <input type="checkbox" id="check<%#Eval("ProductInfo.Id")%>" value="<%#Eval("ProductInfo.Id")%>" name="check<%#Eval("ProductInfo.ShopId")%>"  onclick="CheckProduct('<%#Eval("ProductInfo.ShopId")%>', '<%#Eval("ProductInfo.Id") %>')" />
                            </td>
                            <td>
                                <a href="ProductDetail.aspx?id=<%#Eval("ProductInfo.Id") %>" target="_blank" >
                                    <img alt="产品图片" width="50" src="../Images/accent.png" />
                                </a>
                            </td>
                            <td style="width: 50px;">
                                <%#Eval("ProductInfo.Name") %>
                            </td>
                            <td style="width: 200px;">
                                <div>
                                    <%#Eval("ProductInfo.Title") %>
                                </div>
                                <div>
                                    <%#Eval("ProductInfo.SubTitle") %>
                                </div>
                            </td>
                            <td>
                                <%#Eval("ProductInfo.PricePerUnit") %>
                            </td>
                            <td>
                                <a onclick="ChangeQuantity('sub','quantity<%#Eval("ProductInfo.Id") %>');" href="javascript:void(0);">-</a>
                                <input type="text" value="<%#Eval("Quantity") %>" id="quantity<%#Eval("ProductInfo.Id") %>" style="width: 30px; text-align: right;"/>
                                <a onclick="ChangeQuantity('add','quantity<%#Eval("ProductInfo.Id") %>');" href="javascript:void(0);">+</a> 
                                
                            </td>
                            <td>
                                <%#(decimal)Eval("ProductInfo.PricePerUnit") * (int)Eval("Quantity") %>
                            </td>
                            <td>
                                <a href="/Action/CartItemDelete.aspx?oid=<%#Eval("ProductInfo.Id") %>">删除</a>
                            </td>
                        </tr>
                    </ItemTemplate>
                </asp:Repeater>
                    </table>
                </td>
            </tr>
                
        </ItemTemplate>
    </asp:Repeater>
        </table>
        
        
            
        <div style="width: 173px;background: #f40 none repeat scroll 0 0;
border-color: #f40;

border-radius: 2px;
border-style: solid;
border-width: 1px;
cursor: pointer;
display: block;
font-family: Hiragino Sans GB,microsoft yahei,sans-serif;
font-size: 16px;
line-height: 38px;
text-align: center;">
            <a data-spm-click="gostr=/tbdetail;locaid=d2"    
                 class="J_LinkAdd" title="加入购物车" href="javascript:SubmitCart();"
                style="color: #fff;" >
                <i class="tb-iconfont" style=" font-size: 18px;
font-style: normal;
font-weight: 400;
margin-right: 10px;
vertical-align: bottom;">ŭ</i>
                准备下单
            </a>
        </div>
    </section>

    </form>
</asp:Content>